/**
 * Copyright 2008 by Robert Konigsberg
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the same terms as Perl.
 *
 * @see http://www.perl.com/perl/misc/Artistic.html
 */
package visitor.maze;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * A collection of connected Rooms.
 *
 * Clients cannot call create Maze instances directly. Instead, they should use
 * the {@link MazeBuilder}.
 *
 * @author kberg
 */
public class Maze {
  private final Map<Integer, Room> rooms = new HashMap<Integer, Room>();

  /**
   * Add a room to the maze.
   *
   * @throws IllegalArgumentException if a room with the same id was previously
   * added.
   */
  void addRoom(Room room) {
    if (rooms.containsKey(room.getId())) {
      throw new IllegalArgumentException(
          "Room " + room + " already exists in this.");
    }
    rooms.put(room.getId(), room);
  }

  /**
   * Get the room with the given {@code id} or {@code null} if no such room
   * exists.
   */
  public Room getRoom(int id) {
    return rooms.get(id);
  }

  /**
   * Get an unmodifiable set of all rooms added to this maze, by id.
   */
  public Set<Integer> getRoomIds() {
    return Collections.unmodifiableSet(rooms.keySet());
  }

  void destroy() {
    for (Room room : rooms.values()) {
      room.destroy();
      rooms.clear();
    }
  }
}
